package com.atom.wifiupload;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.atom.wifiupload.adapter.BookAdapter;
import com.atom.wifiupload.bean.FileBean;
import com.atom.wifiupload.gen.FileBeanDao;
import com.atom.wifiupload.utils.CustomDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ============================================================
 * project: WIFI-Upload
 * package: com.atom.wifiupload.wifitransfer
 * fileDescribe:
 * user: admin
 * email: 1299854942@qq.com
 * createTime: 2017/6/24  12:34
 * modify:
 * version:: V1.0
 * ============================================================
 **/
public class BookListFragment extends Fragment {

    @BindView(R.id.rv_booklist)
    RecyclerView mRvBooklist;

    private Unbinder mUnbinder;
    private Context mContext;

    private List<FileBean> mFileBeanList;
    private BookAdapter mBookAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_list, null);
        mUnbinder = ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mRvBooklist.setLayoutManager(new LinearLayoutManager(mContext));
        mRvBooklist.addItemDecoration(new CustomDecoration(mContext, LinearLayoutManager.VERTICAL, Color.parseColor("#DDDDDD"), 1));

        mFileBeanList = new ArrayList<>();
        mBookAdapter = new BookAdapter(mContext, mFileBeanList);
        mRvBooklist.setAdapter(mBookAdapter);
        mRvBooklist.setItemAnimator(new DefaultItemAnimator());
    }

    public void getDataWithDb() {
        List<FileBean> dbFile = WifiBookApp.getDaoInstant().getFileBeanDao().queryBuilder()
                .orderDesc(FileBeanDao.Properties.CreateTime).list();
        if (null != dbFile && !dbFile.isEmpty()) {
            mFileBeanList.clear();
            mFileBeanList.addAll(dbFile);
            mBookAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }
}
